package com.yicong.ants.scenic

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.MutableLiveData
import androidx.navigation.fragment.findNavController
import com.blankj.utilcode.util.BarUtils
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity
import com.yicong.ants.databinding.ScenicOrderListFragmentBinding
import com.yicong.ants.scenic.adapter.ScenicOrderListAdapter
import com.yicong.ants.scenic.data.LoadStateData
import com.yicong.ants.scenic.data.PageData
import com.yicong.ants.scenic.data.TicketOrder
import com.yicong.ants.scenic.viewModel.ScenicViewModel

class ScenicOrderListFragment : Fragment() {
    private var inited: Boolean = false
    private lateinit var binding: ScenicOrderListFragmentBinding
    private val scenicViewModel: ScenicViewModel by activityViewModels()
    private var rootView: View? = null
    private val adapter = ScenicOrderListAdapter()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        if (rootView == null) {
            binding = ScenicOrderListFragmentBinding.inflate(layoutInflater, container, false)
            rootView = binding.root
        }
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        scenicViewModel.ticketOrderList.observe(viewLifecycleOwner) {
            it?.onComplete {
                binding.smartLayout.finishRefresh()
                it.data?.let { data ->
                    data.setLoadedCount(adapter.itemCount)
                    adapter.addData(it.data?.list ?: arrayListOf())
                    if (data.pageInfo?.curPage == data.pageInfo?.countPage) {
                        adapter.loadMoreEnd()
                    } else {
                        adapter.loadMoreComplete()
                    }
                }
                binding.progressBar.hide()
            }?.onError(true) {
                binding.smartLayout.finishRefresh()
                adapter.loadMoreFail()
                binding.progressBar.hide()
            }
        }
        if (inited) return
        inited = true
        BarUtils.setStatusBarLightMode(requireActivity(), false);
        binding.backBtn.setOnClickListener { findNavController().navigateUp() }
        adapter.callback = object : ScenicOrderListAdapter.Callback {
            override fun onReorderBtnClick(item: TicketOrder) {
                findNavController().navigate(
                    ScenicOrderListFragmentDirections.actionScenicOrderListFragmentToScenicDetailFragment(
                        item.scenicID
                    )
                )
            }

            override fun onDetailBtnClick(item: TicketOrder) {
                findNavController().navigate(
                    ScenicOrderListFragmentDirections.actionScenicOrderListFragmentToScenicOrderDetailFragment(
                        item.customerOrderNo
                    )
                )
            }
        }
        adapter.bindToRecyclerView(binding.recyclerView)
        scenicViewModel.getOrderList(requireActivity() as BaseTitleBarActivity<*>, true)
        adapter.refreshListener = {
            scenicViewModel.getOrderList(requireActivity() as BaseTitleBarActivity<*>, true)
        }
        binding.smartLayout.setOnRefreshListener {
            adapter.data.clear()
            scenicViewModel.getOrderList(requireActivity() as BaseTitleBarActivity<*>, true)
        }
    }
}